package com.logic.landseaserver.common.util;

import org.slf4j.Logger;

import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;

public abstract class LoggerHelper
{
    
    private static final Integer LOG_STACKTRACE_LENGTH = 150;
    
    public static String whoInvokedMe(Class<?> c)
    {
        String ret = "UNKNOWN";
        StackTraceElement[] element = Thread.currentThread().getStackTrace();
        if (c != null && element != null)
        {
            for (int i = 0; i < element.length; i++)
            {
                String className = element[i].getClassName();
                if (className.equals(c.getName()))
                {
                    ret = "[" + element[i].getClassName() + "." + element[i].getMethodName() + "()#"
                        + element[i].getLineNumber() + "]";
                    break;
                }
            }
        }
        if (ret != null && ret.length() > LOG_STACKTRACE_LENGTH)
        {
            ret = ret.substring(0, LOG_STACKTRACE_LENGTH);
        }
        return ret;
    }
    
    /**
     * 
     * [简要描述]：当发生异常时，处理日志及返回结果</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-06-29)</br>
     *
     * @param log 日志对象
     * @param methodName 方法名
     * @param e LandseaException异常对象
     * @param map CommonResultMap
     *
     */
    public static void handleException(Logger log, String methodName, LandseaException e, CommonResultMap map)
    {
        if (e.isBusinessException())
        {
            log.error(methodName + " handle failed|" + e.getErrorMsg());
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getErrorDesc());
        }
        else
        {
            log.error(methodName + " handle failed.", e);
            map.setResult(CommonResult.ERROR, e.getErrorDesc());
        }
    }
}
